/*
 * 意见反馈管理
 * @author 曾文
 * @time 2018/9/26
 * @since 1.0.0
 * */
import React from 'react'
import { connect } from 'dva'
import { Form,message,Modal,Input} from 'antd'
import { Link, hashHistory } from 'dva/router'
import FlagBtn from 'widget/FlagBtn/FlagView'
import config from 'config/config'
import { mapStateToProps } from 'utils/view_utils'
import styles from './winningInfoListStyle.less'
import ListPage from 'widget/PageTmpl/ListPage'
import { Button } from 'antd/lib/radio';
import cx from 'classnames'
const { TextArea } = Input;

let subElements = JSON.parse(localStorage.getItem('navBar')).subElements
const routeView = function ({ dispatch, model, form }) {

    const { resetFields, getFieldDecorator, validateFields, setFieldsValue } = form
    const { modelObj, namespace } = model
    let {gameActivityId,biz,showModal,defaultRedeemRemark} = modelObj

    function paramsUrl(obj) {
      let result = '';
      let item;
      for (item in obj) {
        if (obj[item] && String(obj[item])) {
          result += `&${item}=${obj[item]}`;
        }
      }
      if (result) {
        result = '?' + result.slice(1);
      }
      return result;
    }
    function cashPrize(type,item){
      //兑奖方式，1为兑奖，0为取消兑奖
      // defaultRedeemRemark = ''
      // defaultRedeemRemark = item.redeemRemark
      let params = {
        redeemRemark:item.redeemRemark,
        prizeId:item.prizeId,
        redeemFlag:type
      }
      dispatch({
        type: `${namespace}/updateStore`,
        payload: { 
          defaultRedeemRemark:params,
          showModal:true 
        }
    });
    }
    function handleOk(){
      dispatch({
        type: `${namespace}/prizeRedeem`,
        payload: {...defaultRedeemRemark}
    });
    }

    function handleCancel(){
      dispatch({
        type: `${namespace}/updateStore`,
        payload: { showModal:false }
    });
    }
    function changeRedeemRemark(value){
      defaultRedeemRemark.redeemRemark = value
      dispatch({
        type: `${namespace}/updateStore`,
        payload: { 
          defaultRedeemRemark,
        }
    });
    }
    let isPrizeList = [
      {label:'未兑奖',value:0},
      {label:'已兑奖',value:1},
    ]
    // let defaultRedeemRemark=''
      // 分页表格属性配置
  let pageProps = {
    // ---------- 界面部分 ----------
    'ui': {
      // 页面接口地址
      'api_url': 'mideadc-game-center-service/activity/gameActivityUserPrize/list',
      // "api_url": "admin/housetype/list",
      "data_field_name":"result.records",
      "data_pageNo":"result.current",
      "data_pageSize":"result.size",
      "data_total":"result.total",
      // 接口方式
      'method': 'GET',
      'app': 'isales-mgmt-application',
      // 接口参数
      'params': `gameActivityId=${gameActivityId}`,
      // 模拟数据 - 用于开发需要，若填充了上面的api_url之后，请将此项设置为null
      // "mockData": mockData,
      'mockData': null,
      // 查询栏
      'search_bar': {
        'fields': [
          {
            en_name: 'nickname',
            zh_name: '客户名称',
            elem_type: 'Input',
            elem_valid_type: 'string',
          },
          {
            en_name: 'mobilePhone',
            zh_name: '客户号码',
            elem_type: 'Input',
            elem_valid_type: 'string',
          },
          {
            en_name: 'redeemFlag',
            zh_name: '是否兑奖',
            elem_type: 'Select',
            elem_valid_type: 'string',
            cmpt_items:isPrizeList
          },
        ],
        searchHandler: (p) => { // 自定义搜索按钮回调函数，点击搜索时触发
          console.log('search click', p)
        },
        resetHandler: (p) => { // 自定义重置按钮回调函数，点击重置时触发
          console.log('reset click', p)
        },
        searchCallback: params => { // 自定义搜索完成后的后调函数, 搜索完成后触发, 并将本次搜索的参数传入
          console.log('search finish', params)
        },
        pasteTargetList: [  // 列表组件中将输入框的en_name作为默认id(也可以通过id另外设置)
          'carName',  // 将需要粘贴事件拦截的输入框id以数组的形式传入, 该参数对应PasteFromClipboard组件的targetList参数
        ]
      },
      // 数据表格
      'table': {
        'status_params': 'status=0',
        'status_text': '全部',
        'scroll': { x: 2000 },
        'action_props': { width: 200 },
        // 表头字段列表"
        'fields': [
          {
            en_name: "key",
            zh_name: "序号",
            width: 60,
            render: (a, item, index, d, e, f) => {
              return index + 1
            },
          },
          {
            'en_name': 'cityName',
            'zh_name': '城市',
            width: 200
          },
          {
            'en_name': 'nickname',
            'zh_name': '微信昵称',
            width: 100
          },
          {
            'en_name': 'mobilePhone',
            'zh_name': '客户手机',
            width: 100,
          },
          {
            'en_name': 'awardLevel',
            'zh_name': '奖项等级',
            width: 100,
            render: (a, item, index, d, e, f) => {
            return <div>{item.awardLevel}等奖</div>
            }
          },
          {
            'en_name': 'awardName',
            'zh_name': '奖品名称',
            width: 100,
            // render: (a, item, index, d, e, f) => {
            // }
          },
          {
            'en_name': 'createTime',
            'zh_name': '中奖时间',
            width: 100,
          },
          {
            'en_name': 'name',
            'zh_name': '联系人',
            width: 100,
          },
          {
            'en_name': 'mobile',
            'zh_name': '联系手机',
            width: 100,
          },
          {
            'en_name': 'detailAddress',
            'zh_name': '楼盘名称',
            width: 100,
          },
          {
            'en_name': 'redeemFlag',
            'zh_name': '是否已经兑换',
            width: 100,
            render: (a, item, index, d, e, f) => {
              if(item.redeemFlag==0){
                return <div>未兑奖</div>
              }else{
                return <div>已兑奖</div>
            }
          }
          },
          {
            'en_name': 'updatePerson',
            'zh_name': '操作者',
            width: 100,
          },
          {
            'en_name': 'redeemRemark',
            'zh_name': '备注',
            width: 100,
          },
          {
            'en_name': 'edit',
            'zh_name': '操作',
            fixed:'right',
            width: 100,
            render: (a, item, index, d, e, f) => {
              return (
                <span>
                {item.redeemFlag==0&&<FlagBtn node={<a onClick={() => cashPrize('1',item)}>兑奖<span className="ant-divider" style={{ display: 'inline-block' }} /></a>} btnId={'20502'}> </FlagBtn>}
                {item.redeemFlag==1&&<FlagBtn node={<a onClick={() => cashPrize('0',item)}>取消兑奖<span className="ant-divider" style={{ display: 'inline-block' }} /></a>} btnId={'20502'}> </FlagBtn>}
                </span>
              )
            }
          },
          // {
          //   'en_name': 'availCount',
          //   'zh_name': '操作',
          //   width: 300,
          //   render: (a, item, index, d, e, f) => {
          //     return (
          //       <span>
          //         <FlagBtn node={<a onClick={() => giftInfoSkip('check', item)}>预览<span className="ant-divider" style={{ display: 'inline-block' }} /></a>} btnId={'20502'}> </FlagBtn>
          //         <FlagBtn node={<a onClick={() => detailSkip('edit', item.productId)}>发布<span className="ant-divider" /></a>} btnId={'20503'}> </FlagBtn>
          //         <FlagBtn node={<a onClick={() => detailSkip('edit', item.productId)}>玩家<span className="ant-divider" /></a>} btnId={'20503'}> </FlagBtn>
          //         <FlagBtn node={<a onClick={() => detailSkip('edit', item.productId)}>奖品<span className="ant-divider" /></a>} btnId={'20503'}> </FlagBtn>
          //         <FlagBtn node={<a onClick={() => detailSkip('edit', item.productId)}>统计<span className="ant-divider" /></a>} btnId={'20503'}> </FlagBtn>
          //         <FlagBtn node={<a onClick={() => detailSkip('edit', item.productId)}>复制链接<span className="ant-divider" /></a>} btnId={'20503'}> </FlagBtn>
          //         <FlagBtn node={<a onClick={() => detailSkip('edit', item.productId)}>编辑<span className="ant-divider" /></a>} btnId={'20503'}> </FlagBtn>
          //         <FlagBtn node={<a onClick={() => detailSkip('edit', item.productId)}>删除<span className="ant-divider" /></a>} btnId={'20503'}> </FlagBtn>
          //       </span>
          //     )
          //   },
          // },
        ],
      },
      // 操作栏
      action_bar: [
        {
          'func_name': 'add',
          'label': '导出',
          btnId: '20501',
          'type': 'primary',
          'onClick': (e,row) => {
            let query = { ...biz.queryForm,...{gameActivityId}}
            debugger
            // let url = `${window.location.origin}/activity/gameActivityUserPrize/exportXls/${paramsUrl(query)}`
            // // debugger
            // let link = document.createElement("a");
            // link.style.display = "none";
            // link.href = url;
            // // link.setAttribute("download", fileName);
            // document.body.appendChild(link);
            // link.click();
            // document.body.removeChild(link); //下载完成移除元素
            // window.URL.revokeObjectURL(url); //释放掉blob对象
            // message.success('操作成功')
            dispatch({
              type: `${namespace}/winningExportXls`,
              payload: {...query}
            })
          }
        },
      ],
    },
  }
  if (!!modelObj.biz) {
    pageProps.biz = modelObj.biz
    pageProps.biz.syncBackCbf = (biz) => {
      dispatch({
        type: `${namespace}/updateStore`,
        payload: { biz }
      })
    }
  }

    return (
        <div className={cx('pageLayout')}>
            <ListPage pageProps={pageProps} />
            <Modal
            maskClosable={true}
            title={defaultRedeemRemark.redeemFlag==1?'确认已经兑换':'取消兑换'}
            visible={showModal}
            onOk={handleOk}
            onCancel={handleCancel}
            >
            <label>兑换备注</label><TextArea value={defaultRedeemRemark.redeemRemark} onChange={(e) => { changeRedeemRemark(e.target.value) }}></TextArea>
            </Modal>
        </div>
    )
}
export default connect(mapStateToProps)(Form.create()(routeView))
